/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

import java.util.List;
import logging.LogSetup;
import org.apache.log4j.Logger;

/**
 *
 * @author Danielondon
 */
public class GreedyChargeStrategy implements IChargeStrategy{
    protected Logger logger = LogSetup.getLogger();
    private String strategyName;
    private int slotSize;
    private String carName;
    SolverType solverType;
    public GreedyChargeStrategy(int slotSize, String carName, SolverType solverType){
        this.strategyName = "Greedy";
        this.slotSize = slotSize;
        this.carName = carName;
        this.solverType = solverType;
    }
    
    @Override    
    public SimulationResult getResult(IMarket market, IDrivingPattern drivingPattern, 
    StateOfCharge initialStateOfCharge, ICarSpecs carSpecs, int totalSlots) throws UnfeasibleException {
        SimulationResult result = new SimulationResult(carSpecs, totalSlots, drivingPattern.getName(), this.getStrategyName());        
        try {
            //double[] stateOfCharge = new double[totalSlots];
            //double[] consumptionPerSlot = new double[totalSlots];
            double[] costPerSlot = new double[totalSlots];
            double[] chargingPerSlot;        
            //logger.info("Car Specs " + carSpecs.toString());            
            List<int[]> patterns = drivingPattern.getConnectionsByRow();
            // Get the required
            double [] consumptionAggregate = drivingPattern.getConsumptionBlocks(carSpecs);
            ISolver solver;
            if (solverType == SolverType.GLPK){
                solver = new GLPKSolver(this.getStrategyName(), totalSlots, market.getAllPrices(), 
                    drivingPattern.getConnected(), patterns, consumptionAggregate, 
                    carSpecs.getMaxChargingPerSlot(CommonResources.DEFAULT_INTERVAL) );
            } else if (solverType == SolverType.LPSolver){
                solver = new LPSolver(this.getStrategyName(), totalSlots, market.getAllPrices(), 
                    drivingPattern.getConnected(), patterns, consumptionAggregate, 
                    carSpecs.getMaxChargingPerSlot(CommonResources.DEFAULT_INTERVAL) );
            } else{
                throw new Exception("Invalid Solver set");
            }
                
            chargingPerSlot = solver.solve();
            
            for (int i =0; i < totalSlots; i++){
                costPerSlot[i] = chargingPerSlot[i]*market.getPrice(i);
            }
            
            //result.setStateOfEnergy(stateOfCharge);
            //result.setConsumption(consumptionPerSlot);
            
            result.setCost(costPerSlot);
            result.setCharging(chargingPerSlot);
            result.setMarket(market.getAllPrices());
            
        } catch (Exception ex) {
            logger.error("Error calculating the greedy strategy with solver " + this.solverType, ex);
            result = null;
        }
        return result;
    }    

    /**
     * @return the strategyName
     */
    @Override
    public String getStrategyName() {
        return strategyName+"_"+carName;
    }

    @Override
    public int getSlotSize() {
        return this.slotSize;
    }
}
